package com.financing.controller.invest;
import java.util.List;
import java.util.Map;

import com.financing.model.Data;
import com.financing.model.invest.ShareBenefit;
import com.financing.model.user.User;
import com.financing.service.invest.ShareBenefitService;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Money;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**	
 * 交易控制器
 */
public class ShareBenefitController extends Controller {
	private static ShareBenefitService shareBenefit = new ShareBenefitService();

	/** 
	 * 获取我的交易记录列表
	 */  
	public void myList(){
		User u = getAttr("user");
		int page = getParaToInt("page",1); 
		int size = getParaToInt("size",10);
		Map<String,String> params = Data.flat(getParaMap());
		params.put("agentId", String.valueOf(u.getId()));
		Page<ShareBenefit> p = shareBenefit.list(page,size,params);
		double total = Money.format(shareBenefit.sumByUser(u.getId()));
		renderJson(Json.ok("数据获取成功！", Data.put("page", p, "total", String.valueOf(total))));
	} 
	
	/**
	 * 获取我的交易记录列表
	 */ 
	public void groupList() {
		User u = getAttr("user");
		String start = getPara("start", DateUtil.format("yyyy-MM")+"-01 00:00:00");
		String end = getPara("end", DateUtil.format("yyyy-MM")+"-31 24:00:00");
		
		// 获取组别总收益 
		List<ShareBenefit> list = shareBenefit.findByTotalGroup(u.getId());
		list.forEach(n->{
			ShareBenefit sb = shareBenefit.findByGroup(u.getId(),n.getUserId(),start,end);
			if(sb!=null) {
				n.put("range_amount", sb.getDouble("total_amount"));
				n.put("range_trade_amount", sb.getDouble("total_trade_amount"));
			}
		});
		renderJson(Json.ok("数据获取成功！", Data.put("list", list)));
	} 
	
	/**
	 * 获取交易记录列表
	 */
    @Clear
	public void list() {
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10); 
		Page<ShareBenefit> p = shareBenefit.list(page,size,Data.flat(getParaMap()));
		renderJson(Json.ok("数据获取成功！", Data.put("page", p)));
	} 
}
 